package com.beijiteshop.shop.utils

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.beijiteshop.libcommon.utils.DensityUtil
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.beijiteshop.shop.databinding.ItemRecProductBinding

/**
 * 首页列表分割线
 */
class HomeGridItemDecoration(private val space: Int) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        if (parent.getChildViewHolder(view) is BaseDataBindingHolder<*>){
            val item = (parent.getChildViewHolder(view) as BaseDataBindingHolder<*>).dataBinding as? ItemRecProductBinding
            item?.let {_ ->
                val layoutParams: GridLayoutManager.LayoutParams? = view.layoutParams as? GridLayoutManager.LayoutParams
                layoutParams?.let {
                    if(it.spanIndex % 2 == 0){
                        outRect.right = DensityUtil.dp2px(parent.context,space/2)
                        outRect.left = DensityUtil.dp2px(parent.context,space)
                    }else{
                        outRect.left = DensityUtil.dp2px(parent.context,space/2)
                        outRect.right = DensityUtil.dp2px(parent.context,space)
                    }
                }
            }
        }

    }
}